<?php
class Vmg_Plugin_Authorization extends Zend_Controller_Plugin_Abstract
{
    public function routeStartup(Zend_Controller_Request_Abstract $request)
    {
       
    }

    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
    	
       
    }

    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
       
    }

    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
    	try{
	    	if($this->getRequest()->getControllerName() != "index"){
		    	// check permission
				$auth = Zend_Auth::getInstance();
				if($auth->hasIdentity()){ // one user has logged in his session
					$user = $auth->getIdentity();
					if(!$user->authorize($this->getRequest()->getControllerName(), $this->getRequest()->getActionName())){
						// not authorize redirect to his default page
						$user_default_values = $user->getDefaultValues();						
						$request->setControllerName($user_default_values["controller"]);
            			$request->setActionName($user_default_values["action"]);            			 				
					}
				} else {
					$request->setActionName("login");
					$request->setControllerName("index");
				}
	    	}
    	}catch (Exception $e) {
            $request->setControllerName("error");           
            $request->setActionName("error");
        }
    	
    	
    }

    public function postDispatch(Zend_Controller_Request_Abstract $request)
    {
        
    }

    public function dispatchLoopShutdown()
    {
        
    }
}

?>